<html>

	<head>
		<title>订单详情管理</title>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">

		<link href="../../css/skin/default/style.css" rel="stylesheet" type="text/css" />
		<link rel="stylesheet" href="../../css/dialog.css">
		<link href="../../js/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />

		<script src="../../js/lib/jquery/jquery-1.10.2.min.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>

		<script src="../../js/lib/ligerUI/js/plugins/ligerPopupEdit.js"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerLayout.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerGrid.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerForm.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerCheckBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerComboBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDateEditor.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerRadio.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTextBox.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerSpinner.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTree.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDialog.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerDrag.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerResizable.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerTip.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerMenu.js" type="text/javascript"></script>
		<script src="../../js/lib/ligerUI/js/plugins/ligerToolBar.js" type="text/javascript"></script>
		<script src="../../js/lib/jquery.form.js" type="text/javascript"></script>
		<script src="../../js/lib/json2.js" type="text/javascript"></script>
		<script src="../../js/dialog.js"></script>
		<script src="../../js/JS/layout.js"></script>
		<script src="../../js/constants.js" type="text/javascript"></script>
		<script src="../../js/api.js" type="text/javascript"></script>

		<script type="text/javascript">
			var manager;
			var Fcode = $api.getparastr("Fcode");
			var saleUrl = BaseUrl + "Sale?token=" + $api.getStorage("token");
			$(function() {
				$("form").ligerForm();

				var url = saleUrl + "&Fcode=" + Fcode;
				$api.ajaxget(url, {}, function(res) {
					if(!res.Head) {
						top.$.ligerDialog.error("订单不存在！", function() {
							windows.History.go(-1);
						});
					} else {
						if(res.Head.Fstatus == 1) {
							top.$.ligerDialog.error("该订单尚未完成付款！", function() {
								windows.History.go(-1);
							});
							return;
						}
						if(res.Head.Faid != 0 && Fastatus == 0) {
							top.$.ligerDialog.error("进行中,活动订单不能退款！", function() {
								windows.History.go(-1);
							});
							return;
						}
						if(res.Head.Fstatus == 5) {
							top.$.ligerDialog.error("该订单已经完成不能退款！", function() {
								windows.History.go(-1);
							});
							return;
						}

						initGrid(res.Detail);
						initView(res);
					}

				});

			});

			function initGrid(data) {

				var rows = {
					"Rows": data,
					"Total": data.length
				}
				manager = $("#maingrid").ligerGrid({
					columns: [{
							display: '标题',
							name: 'PDesc',
							align: 'left',
							minWidth: 280,
							render: function(rowdata, rowindex, value) {
								var Fattr = "";
								var arr;
								if(rowdata.Fattr) {
									arr = JSON.parse(rowdata.Fattr);
								}
								for(var i in arr) {
									var row = arr[i];
									Fattr += row.Fattr + ":" + row.Fvalue + "；";
								}
								var ret = "<img src=" + rowdata.Fimg + " class='listimgs' style='float:left;top:5px' />";
								ret += "<div style='float:left; margin-left:5px'><span>" + rowdata.Fpname + "</span>";
								ret += "<br/><span style='color:#999'>" + Fattr + "</span>"

								ret += "</div>"
								return ret;
							}
						},
						{
							display: '货号',
							name: 'Fpcode',
							align: 'left',
							width: 120,
							isSort: true
						},
						{
							display: '条码',
							name: 'Fbarcode',
							align: 'left',
							width: 120
						},
						{
							display: '单价',
							name: 'Fprice',
							type: 'currency',
							width: 100,
							align: 'right'

						},
						{
							display: '待发数量',
							name: 'Fnum',
							type: 'number',
							width: 100,
							align: 'right',
							render: function(rowdata, rowindex, value) {
								return rowdata.Fnum - rowdata.Fsendnum;
							}
						},
						{
							display: '本次发送数量',
							name: 'Fnsendnum',
							type: 'number',
							width: 100,
							align: 'left', 
							editor: {
								type: 'number',
								initSelect: true
							}
						}

					],
					data: rows,
					width: '99.5%',
					isScroll: true,
					rowHeight: 100,
					enabledEdit: true

				});
			}

			function initView(rowdata) {
				var Fdate = rowdata.Head.Fdate = rowdata.Head.Fdate.replace("T", " ");
				var i = rowdata.Head.Fdate.indexOf('.');
				rowdata.Head.Fdate = rowdata.Head.Fdate.substr(0, i);

				var deal = "<span style='margin-left:5px'>交易流水号:</span><span style='color:#333'>" + rowdata.Head.Fcode + "</span>";

				deal += "<span style='margin-left:5px'>订单金额:￥</span><span id='Ftotal' style='color:red;font-size:22px'>" + rowdata.Head.Ftotalmoney + "</span>";
				deal += "<span style='margin-left:5px'>状态:【" + noteStatus(rowdata) + "】</span>"
				deal += "<span style='margin-left:5px;color:#333'>下单时间:" + Fdate + "</span>";
				deal += "<span style='margin-left:5px'>购买人:" + unescape(rowdata.Head.Fnickname) + "[" + rowdata.Head.Fmobile + "]</span> ";
				$("#deal").append(deal);

				var address = "<tr><td>收件信息：" + rowdata.Head.Fprovince + rowdata.Head.Fcity + rowdata.Head.Fzone + rowdata.Head.Faddress + "</td></tr>";
				address += "<tr><td style='font-size:12px;color:#999;padding-left:78px;'>" + rowdata.Head.Fname + "[" + rowdata.Head.Fmobile + "]</td></tr>";
				if(!$api.isEmpty(rowdata.Head.Fmemo))
					address += "<tr><td>买家留言:" + rowdata.Head.Fmemo + "</td></tr>";

				$("#address").append(address);

			}

			function noteStatus(rowdata) {
				if(rowdata.Head.Fstatus == 2)
					return "待发货";
				if(rowdata.Head.Fstatus == 3)
					return "部分发货";
				if(rowdata.Head.Fstatus == 4)
					return "待确认收货";
				if(rowdata.Head.Fstatus == 5)
					return "已完成";
				if(rowdata.Head.Fpstype == 1) {
					if(rowdata.Head.Fpsstatus == 1)
						return "部分发货";
					else if(rowdata.Head.Fpsstatus == 2)
						return "全部发货";
					else if(rowdata.Head.Fpsstatus == 3)
						return "已收货";
				} else {
					if(rowdata.Head.Fpsstatus == 1)
						return "已发货";
					else if(rowdata.Head.Fpsstatus == 2)
						return "已签收"

				}
			}
			//发送包裹
			function doSave() {
				var arr = [];
				for(var i in manager.rows) {
					var row = manager.rows[i];
					if(row.Fnsendnum > row.Fnum) {
						$.ligerDialog.warn("发货数量不能大于订单数量");
						return;
					}
					if(row.Fnum >= row.Fnsendnum && row.Fnsendnum && row.Fnsendnum > 0) {
						arr.push({
							"Fid": row.Fid,
							"Fpid": row.Fpid,
							"Ftid": row.Ftid,
							"Fnum": row.Fnsendnum
						});
					}
				}
				if(arr.length == 0) {
					$.ligerDialog.warn("请输入需要发送的数量");
					return;
				}

				var url = "vstore/tb_sale/Mail.html";
				parent.openCommWindow3(url, '邮寄信息', 500, 300, function(res) {
					var url = BaseUrl + "SaleDelivery";
					var param = {
						"Token": $api.getStorage("token"),
						"Fsalecode": Fcode,
						"Fmid": res.Fmid,
						"Fcode": res.Fcode,
						"Fztid": 0,
						"Detail": arr
					}
					$api.ajaxapp2(url, param, function(data) {
						top.$.ligerDialog.error("发货完成！", function() {
							window.history.go(-1);
						});
					});

				});

			}
		</script>

	</head>

	<body class="mainbody">
		<form id="form1" onsubmit="return false">
			<!--导航栏-->
			<div class="location" width="300px">
				<table>
					<tr>
						<td>
							<!--<a href="javascript:history.back(-1)" class="back"><i></i><span>返回</span></a>-->
						</td>
						<td>
							<span style="margin-left:10px">交易管理</span>
							<i class="arrow"></i>
							<span class="nows">发送包裹</span> </td>
					</tr>
				</table>
			</div>

			<div class="line10"></div>
			<!--/导航栏-->
			<div class="div-content">
				<div class="divhead">订单修改</div>
				<hr class="divline" />
				<div class="alarm">
					友情提示：填写<span style="color:red">本次发货数量</span>进行发货操作，修改完成后保存成功后生效！
				</div>
				<div class="toolbar">
					<!--支付方式-->

					<div style="margin-left:15px;font-size:12px;line-height:30px;">
						<table cellspacing="4px" cellpadding="4px" id="address">
						</table>
					</div>
					<!--按钮 -->
					<div style="line-height:30px;float:left;margin-left:15px;">
						<a onclick="doSave();" class="btn green">发货</a>
					</div>
				</div>
			</div>
			<div id="deal" style="padding: 3px; background: #f3f3f3;width:99%;font-size:13px;line-height:22px;border: 1px solid #ddd;"></div>

			<!--订单详情-->
			<div id="maingrid" style="margin: 0px;background:#fff;"></div>
			</div>
		</form>

	</body>

</html>